let axios = require('axios');
let cheerio = require('cheerio')
const fs = require('fs')
const url = require('url')
const path = require('path')

let httpUrl = 'https://www.doutula.com/article/list/?page=1'

axios.get(httpUrl).then(function (res) {
    // console.log(res.data)
    let $ = cheerio.load(res.data)
    $('#home .col-sm-9>a').each((i, el) => {
        let pageUrl = $(el).attr('href')
        let title = $(el).find('.random_title').text()
        let reg = /(.*?)\d/igs
        title = reg.exec(title)[1]
        fs.mkdir('./img/' + title, function (err) {
            if (err) {
                // console.log(err)
            } else {
                console.log('成功创建目录：', './img/' + title)
            }
        })
        // console.log(title)
        parsePage(pageUrl,title) 
    })
}).catch(function (err) {
    console.log(err)
})

async function parsePage(url,title) {
    let res = await axios.get(url)
    let $ = cheerio.load(res.data)
    $('.pic-content img').each((i, el) => {
        let imgUrl = $(el).attr('src')
        console.log(imgUrl)
        // 解析图片扩展名
        extName = path.extname(imgUrl)

        //图片写入的路径和名字
        let imgPath = `./img/${title}/${title}-${i}${extName}`
        let ws = fs.createWriteStream(imgPath)

        axios.get(imgUrl, { responseType: 'stream' }).then(function(res) {
            res.data.pipe(ws)
            // console.log('图片加载完成')
            res.data.on('close', function () {
                ws.close()
            })
        })
    })
}
